/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmijcoltrane;

/**
 *
 * @author Felipe Gutierrez
 */


import java.util.Stack;

import net.sf.jColtrane.annotations.methods.BeforeElement;
import net.sf.jColtrane.annotations.methods.EndElement;
import net.sf.jColtrane.annotations.methods.StartElement;
import net.sf.jColtrane.handler.ContextVariables;
import net.sf.jColtrane.handler.SAXEvent;

import xmibase.XmiAssociation;
import xmibase.XmiAttribute;
import xmibase.XmiClass;
import xmibase.XmiPackage;
import xmibase.XmiTagDefinition;
import xmibase.XmiTagValue;

public class XmiGetTagDefinition {
	public XmiPackage rootPackage;
	public Stack elements;
	public boolean debug = false;

	public XmiGetTagDefinition(XmiPackage packages, Stack elements) {

		this.elements = elements;
		this.rootPackage = packages;

	}
	public void printDebug(String msg) {
		if (debug) {
			System.out.println(msg);
		}
	}
	/**
	 * start a UML:TagDefenition element of the UML:Package
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML:Namespace.ownedElement")
	@StartElement(tag = "UML:TagDefinition")
	public void startTagDefenitionPackage(ContextVariables contextVariables) {

		SAXEvent event = contextVariables.getLastEvent();

		XmiTagDefinition xmiTagDefenition = new XmiTagDefinition();

		xmiTagDefenition.setId(event.getAtributesHolder().getValue("xmi.id"));
		xmiTagDefenition.setName(event.getAtributesHolder().getValue("name"));

		this.rootPackage.addTagDefenition(xmiTagDefenition);
	}

	/**
	 * start a UML:TagDefenition element of the UML:Class
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 1, tag = "UML:ModelElement.taggedValue")
	@StartElement(tag = "UML:TaggedValue")
	public void startTagValueClass(ContextVariables contextVariables) {
		printDebug("     Start TagValue");

		SAXEvent event = contextVariables.getLastEvent();

		XmiTagValue xmiTagValue = new XmiTagValue();

		xmiTagValue.setId((event.getAtributesHolder().getValue("xmi.id")));

		this.elements.push(xmiTagValue);
		contextVariables.getGeneralUseMap().put(xmiTagValue.getId(),
				xmiTagValue);

	}

	/**
	 * start a UML:TagValue element of the UML:Class
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 2, tag = "UML:ModelElement.taggedValue")
	@EndElement(tag = "UML:TaggedValue.dataValue")
	public void endTagDataValueClass(ContextVariables contextVariables) {
		printDebug("        Start TagValueData");
		// pega a tagvalue no mapa
		XmiTagValue xmiTagValue = (XmiTagValue) this.elements.peek();
		// seta o valor do marcador
		xmiTagValue.setTagValue(contextVariables.getBody());
	}

	@BeforeElement(elementDeep = 3, tag = "UML:ModelElement.taggedValue")
	@StartElement(tag = "UML:TagDefinition")
	public void startTagDefinitionClass(ContextVariables contextVariables) {

		printDebug("        Start TagDefinition");

		SAXEvent event = contextVariables.getLastEvent();


		XmiTagValue xmiTagValue = (XmiTagValue) this.elements.peek();
		XmiTagDefinition xmiTagDefenition = new XmiTagDefinition();

		xmiTagDefenition.setId((event.getAtributesHolder()
				.getValue("xmi.idref")));

		xmiTagValue.setXmiTagDefenition(xmiTagDefenition);

	}

	@BeforeElement(elementDeep = 1, tag = "UML:ModelElement.taggedValue")
	@EndElement(tag = "UML:TaggedValue")
	public void endTagValueClass(ContextVariables contextVariables) {


		XmiTagValue tagValue = (XmiTagValue) this.elements.peek();
		this.elements.pop();

		if (!(this.elements.empty())) {
			Object obj = this.elements.peek();

			// adiciona a tagValue no pacote, classe ou atributo
			if (obj instanceof XmiPackage) {
				XmiPackage pkg = (XmiPackage) obj;
				pkg.addTagValue(tagValue);
			} else if (obj instanceof XmiClass) {
				XmiClass classe = (XmiClass) obj;
				classe.addTagValue(tagValue);
			} else if (obj instanceof XmiAttribute) {
				XmiAttribute att = (XmiAttribute) obj;
				att.addTagValue(tagValue);
			} else if (obj instanceof XmiAssociation) {
				XmiAssociation associaiton = (XmiAssociation) obj;
				associaiton.addTagValue(tagValue);
			}
			printDebug("     End TagValue");
		} else {
			this.rootPackage.addTagValue(tagValue);
		}

	}

}

